package com.situ.water_pay.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.util.DateUtil;
import com.situ.util.ResponseData;
import com.situ.water_Invoice.pojo.IvInvoice;
import com.situ.water_Invoice.service.IvInvoiceService;
import com.situ.water_pay.pojo.PyBill;
import com.situ.water_pay.service.PyBillService;
import com.situ.water_pay.vo.FundRecovery;
import com.situ.water_system.mapper.SyAreaMapper;
import com.situ.water_system.pojo.SyArea;
import com.situ.water_system.pojo.SyEmp;
import com.situ.water_user.pojo.UsUser;
import com.situ.water_user.service.UsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.Month;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 水费单
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/py-bill")
public class PyBillController {
    @Autowired
    private UsUserService usUserService;
    @Autowired
    private IvInvoiceService ivInvoiceService;
    @Autowired
    private PyBillService pyBillService;
    @Autowired
    private SyAreaMapper syAreaMapper;

    /**
     * 条件查询用户信息
     *
     * @param usUser
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/findUser")
    public ResponseData findUser(UsUser usUser, @RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "6") Integer limit) {
        QueryWrapper<UsUser> wrapper = new QueryWrapper<UsUser>();
        if (usUser != null) {
            if (usUser.getUserno() != null && !"".equals(usUser.getUserno())) {
                wrapper.eq("UserNo", usUser.getUserno());
            }
            if (usUser.getAbc() != null && !"".equals(usUser.getAbc())) {
                wrapper.eq("abc", usUser.getAbc());
            }
            if (usUser.getPhone() != null && !"".equals(usUser.getPhone())) {
                wrapper.eq("Phone", usUser.getPhone());
            }
            if (usUser.getSmsphone() != null && !"".equals(usUser.getSmsphone())) {
                wrapper.eq("SMSPhone", usUser.getSmsphone());
            }
            if (usUser.getDocNum() != null && !"".equals(usUser.getDocNum())) {
                wrapper.eq("DocNum", usUser.getDocNum());
            }
            if (usUser.getUsername() != null && !"".equals(usUser.getUsername())) {
                wrapper.like("UserName", usUser.getUsername());
            }
            if (usUser.getAddress() != null && !"".equals(usUser.getAddress())) {
                wrapper.like("Address", usUser.getAddress());
            }
        }
        Page<UsUser> userPage = new Page<UsUser>(page, limit);
        usUserService.page(userPage, wrapper);
        return ResponseData.ok().putDataValue("list", userPage);
    }

    /**
     * 查询未使用的发票号
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/findIvInvoice")
    public ResponseData findIvInvoice(@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "156") Integer pageSize) {
        Page IvInvoicePage = new Page(page, pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Used", 0);
        List<IvInvoice> IvInvoicelist = ivInvoiceService.page(IvInvoicePage, wrapper).getRecords();
        return ResponseData.ok().putDataValue("list", IvInvoicelist);
    }

    /**
     * 根据用户编号查询水费单
     *
     * @param UserNo
     * @return
     */
    @GetMapping("/findPybillByUserNo")
    public ResponseData findPybillByUserNo(String UserNo) {
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("UserNo", UserNo);
            wrapper.eq("Balance", 0);
            //查询用户未交费的缴费单
            List<PyBill> billList = pyBillService.list(wrapper);
            //查询用户信息
            UsUser usUser = usUserService.getById(UserNo);
            return ResponseData.ok().putDataValue("billList", billList).putDataValue("usUser", usUser);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询水费单失败");
        }
    }

    /**
     * 缴费
     *
     * @param pyBill
     * @return
     */
    @PostMapping("/payment")
    public ResponseData payment(PyBill pyBill, HttpSession session) {
        try {
            SyEmp emp = (SyEmp) session.getAttribute("");
            pyBillService.payment(pyBill, 12);
            return ResponseData.Success("缴费成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("缴费失败");
        }
    }

    /**
     * 修改用户信息
     *
     * @param usUser
     * @return
     */
    @PostMapping("/updateUser")
    public ResponseData updateUser(UsUser usUser) {
        try {
            UsUser user = usUserService.getById(usUser.getUserno());
            user.setPhone(usUser.getPhone());
            user.setSmsphone(usUser.getSmsphone());
            user.setAddress(usUser.getAddress());
            boolean b = usUserService.updateById(user);
            if (b) {
                return ResponseData.Success("修改成功").putDataValue("user", user);
            } else {
                return ResponseData.Success("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("修改用户信息失败");
        }

    }

    /**************************************欠费报表**********************************************/

    /**
     * 查询水费单起始年月-截止年月
     *
     * @return
     */
    @GetMapping("/getYearMonth")
    public ResponseData getYearMonth() {
        try {
            //查询时间
            Date max = pyBillService.getMaxDate();
            Date min = pyBillService.getMinDate();
            List<String> between = DateUtil.getMonthBetween(min, max);
            return ResponseData.ok().putDataValue("betweenList", between);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询水费单起始年月-截止年月失败");
        }

    }

    /**
     * 查询表册信息
     *
     * @return
     */
    @GetMapping("/getRdVolume")
    public ResponseData getRdVolume() {
        try {
            List<SyArea> RdVolumeList = syAreaMapper.selectList(null);
            return ResponseData.ok().putDataValue("RdVolumeList", RdVolumeList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询表册信息失败");
        }
    }

    /***
     * 用户欠费情况查询
     * @param Volumes
     * @param minDate
     * @param maxDate
     * @param SortField
     * @param page
     * @param pageSize
     * @return
     */
    @PostMapping("/getUserArrears")
    public ResponseData pageQquery(String[] Volumes, String minDate, String maxDate, String SortField, @RequestParam(defaultValue = "1", required = false) Integer page, @RequestParam(defaultValue = "5", required = false) Integer pageSize) {
        try {
            Page<PyBill> billPage = new Page<>(page, pageSize);
            IPage<PyBill> Pypage = pyBillService.getUserArrears(billPage,Volumes,minDate,maxDate,SortField);
            return ResponseData.ok().putDataValue("PyBillpage",Pypage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("用户欠费情况查询失败");
        }
    }

    /****************************月资金回收情况报表***********************************/
    /**
     * 查询月资金回收情况
     *
     * @param year
     * @return
     */
    @GetMapping("/getFundRecovery")
    public ResponseData getFundRecovery(String year) {
        try {
            List<FundRecovery> recoveryList = pyBillService.getFundRecovery(year);
            return ResponseData.ok().putDataValue("recoveryList", recoveryList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询月资金回收情况失败");
        }
    }

    /*********************************用户预收费情况报表*******************************/
    @GetMapping("/getUserProjectedFees")
    public ResponseData getUserProjectedFees(Integer amount) {
        try {
            List<FundRecovery> userProjectedFees = pyBillService.getUserProjectedFees(amount);
            return ResponseData.ok().putDataValue("userProjectedFees", userProjectedFees);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询用户预收费情况失败");
        }
    }
}

